
const { menu_controller_save,
    menu_controller_find, menu_controller_delete,
    menu_controller_get } = require("../../../controller/system/menu/MENU.CONTROLLER");
const { serverError } = require("../../../utils");
const { emptyCheck } = require("../../../utils/checkEmpty");
const { getMenuTree, getPermissionConfigTree } = require("../../../utils/getMenuTree");
const pageSizeList = require("../../../utils/slicePageSize");

/**
 * @description 菜单路由
 * @author 韩
 */
const router = require("koa-router")()
router.prefix("/system/menu");

/**@description 添加或编辑 */
router.post("/create", async (ctx, next) => {
    try {
        console.log("发过来的参数body", ctx.request.body)
        const {        // shopId,
            id,
            activeMenuId = undefined,
            visible = undefined,
            parentId = undefined,
            menuName = undefined,
            menuType = undefined,
            menuIcon = undefined,
            routerPath = undefined,
            sort = undefined,
            componentName = undefined,
            componentPath = undefined,
            permissionMark = undefined,
            status = undefined,
        } = ctx.request.body
        const check = emptyCheck({ menuName, menuType, parentId, })
        if (check.code !== 0) {
            ctx.body = check
        } else {
            const res = await menu_controller_save({
                id: id,
                activeMenuId,
                visible,
                parentId,
                menuName,
                menuType,
                menuIcon,
                routerPath,
                sort,
                componentName,
                componentPath,
                permissionMark,
                status
            })
            ctx.body = res
        }
    } catch (error) {
        console.log(error)
        ctx.body = serverError
    }
})

/**@description 获取菜单树 */
router.post("/list", async (ctx, next) => {
    try {
        const { list, totalCount } = await menu_controller_find({ pageSize: 100, current: 1 });
        const res = getMenuTree(list, "0")
        ctx.body = {
            code: 0,
            msg: "",
            data: {
                totalCount: res.length,
                treeData: [
                    {
                        id: "0",
                        activeMenuId: "",
                        visible: true,
                        menuName: "主目录",
                        parentId: "xx",
                        menuType: "1",
                        menuIcon: "",
                        routerPath: "",
                        sort: 1,
                        componentName: "Main",
                        componentPath: "Main",
                        permissionMark: "Main",
                        status: 1,
                        children: res
                    }
                ]
                // menu: res
            }
        };
    } catch (error) {
        console.log(error);
        ctx.body = serverError
    }
})


/**@description 根据id获取菜单详情 */
router.get("/get", async (ctx, next) => {
    try {
        const id = ctx.query.id || undefined;
        const res = await menu_controller_get({ id });
        console.log("查询：\n", id)
        ctx.body = res;
    } catch (error) {
        ctx.body = serverError
    }
})

/**@description 根据id删除菜单 */
router.delete("/delete", async (ctx, next) => {
    try {
        const id = ctx.query.id || undefined;
        const res = await menu_controller_delete({ ids: [id] });
        console.log("查询：\n", id)
        ctx.body = res;
    } catch (error) {
        ctx.body = serverError
    }
})


/**@description 获取全部权限树配置 */
router.post("/getPermissionConfig", async (ctx, next) => {
    try {
        const { list, totalCount } = await menu_controller_find({});
        // console.log("查询：\n", tree)
        // const list = pageSizeList({ data: tree, pageSize: 100, current: 1 })
        const permissions = getPermissionConfigTree(list, "0")
        ctx.body = {
            status: "0000",
            message: "查询成功",
            data: {
                permissions: permissions
            }
        };
    } catch (error) {
        console.log(error);
        ctx.body = serverError
    }
})



// 删除
router.post("/delete", async (ctx, next) => {
    try {
        const { ids = [] } = ctx.request.body
        if (ids.length === 0) {
            ctx.body = {
                status: '0011',
                message: "未选择需要删除的数据"
            }
        }
        else {
            const res = await menu_controller_delete({ ids: ids })
            ctx.body = res;
        }
    } catch (error) {
        ctx.body = serverError
    }
})



module.exports = router